package com.ssm.controller;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.ssm.service.CaseObjService;

@RestController
@RequestMapping("upload")
public class FileUpLoadController {

	private Logger logger = Logger.getLogger(FileUpLoadController.class);

	@Autowired
	private CaseObjService caseObjService;

	@RequestMapping("case/excel/{product}/{version}")
	@ResponseBody
	public Object caseExcelUpLoad(@RequestParam("file") MultipartFile file, HttpServletRequest request, 
			@PathVariable("product") String product, @PathVariable("version") String version) {

		JSONObject res = new JSONObject();

		File serverFile = new File(
				 request.getServletContext().getRealPath("/") + "/TEMP/CASEXCEL/" + file.getOriginalFilename());
		
		try {
			file.transferTo(serverFile);
		} catch (IllegalStateException | IOException e) {
			logger.error("upload file: " + e);
		}
		
		logger.info("upload file: " + serverFile.getAbsolutePath());

		res = caseObjService.importFromExcel(serverFile, product, version);

		return res;
	}
}
